package com.android.jingweiapp.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.jingweiapp.R;
import com.android.jingweiapp.fragment.InformationFragment;
import com.android.jingweiapp.fragment.InterctFragment;
import com.android.jingweiapp.fragment.PublicInfoFragment;
import com.android.jingweiapp.fragment.TopicFragment;
import com.android.jingweiapp.widget.MyToast;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.ly_container)
    FrameLayout lyContainer;
    @Bind(R.id.ly0)
    LinearLayout ly0;
    @Bind(R.id.ly1)
    LinearLayout ly1;
    @Bind(R.id.ly2)
    LinearLayout ly2;
    @Bind(R.id.ly3)
    LinearLayout ly3;

    private InformationFragment informationFragment;
    private InterctFragment interctFragment;
    private PublicInfoFragment publicInfoFragment;
    private TopicFragment topicFragment;
    private static  boolean isExist=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        init();
    }

    /**
     * 点击两次退出程序
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            exit();
            return  false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExist) {
            isExist = true;
            MyToast.getInstance().show("再次点击退出");
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0,4000);
        } else {
            finish();
            System.exit(0);
        }

    }
    Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExist=false;
        }
    };

    private void init() {
        informationFragment = new InformationFragment();
        interctFragment = new InterctFragment();
        publicInfoFragment = new PublicInfoFragment();
        topicFragment = new TopicFragment();

        getSupportFragmentManager().beginTransaction()
                .add(R.id.ly_container, topicFragment).hide(topicFragment)
                .add(R.id.ly_container, publicInfoFragment).hide(publicInfoFragment)
                .add(R.id.ly_container, interctFragment).hide(interctFragment)
                .add(R.id.ly_container, informationFragment).hide(informationFragment)
                .commit();

        ly0.setSelected(true);
        ly0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction()
                        .show(informationFragment)
                        .hide(publicInfoFragment)
                        .hide(interctFragment)
                        .hide(topicFragment).commit();
                ly0.setSelected(true);
                ly1.setSelected(false);
                ly2.setSelected(false);
                ly3.setSelected(false);
            }
        });

        ly1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction()
                        .show(publicInfoFragment)
                        .hide(topicFragment)
                        .hide(interctFragment)
                        .hide(informationFragment).commit();
                ly0.setSelected(false);
                ly1.setSelected(true);
                ly2.setSelected(false);
                ly3.setSelected(false);
            }
        });

        ly2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction()
                        .show(interctFragment)
                        .hide(publicInfoFragment)
                        .hide(informationFragment)
                        .hide(topicFragment).commit();
                ly0.setSelected(false);
                ly1.setSelected(false);
                ly2.setSelected(true);
                ly3.setSelected(false);

            }
        });

        ly3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction()
                        .show(topicFragment)
                        .hide(publicInfoFragment)
                        .hide(interctFragment)
                        .hide(informationFragment).commit();
                ly0.setSelected(false);
                ly0.setSelected(false);
                ly2.setSelected(false);
                ly3.setSelected(true);
            }
        });

        ly0.callOnClick();

    }

}
